//
//  WKUsernameLoginVM.m
//  WuKongUsernameLogin
//
//  Created by tt on 2023/9/9.
//

#import "WKUsernameLoginVM.h"
#import "WKMnemonicsVC.h"
#import "WKUsernameRegisterVC.h"
#import "WKLoginResp.h"
#import <WuKongUsernameLogin/WuKongUsernameLogin-Swift.h>
#import "WKMnemonicsImportVC.h"
#import "WKForgetPasswordVC.h"
@interface WKUsernameLoginVM ()

@property(nonatomic,copy) NSString *username;
@property(nonatomic,copy) NSString *password;

@end

@implementation WKUsernameLoginVM

- (NSArray<NSDictionary *> *)tableSectionMaps {
    __weak typeof(self) weakSelf = self;
    return @[
        @{
            @"height":@(40.0f),
            @"items":@[
                    @{
                        @"class":WKLabelModel.class,
                        @"text":LLang(@"账号登录"),
                        @"textColor": [WKApp shared].config.defaultTextColor,
                        @"font": [[WKApp shared].config appFontOfSizeSemibold:18.0f],
                        @"center":@(true),
                    },
            ],
        },
        @{
            @"height":@(40.0f),
            @"items":@[
                    @{
                        @"class":WKTextFieldItemModel.class,
                        @"placeholder":LLang(@"请填写账号"),
                        @"label": @"账号",
                        @"showBottomLine":@(true),
                        @"onChange": ^(NSString *value) {
                            weakSelf.username = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(0.01f),
            @"items":@[
                    @{
                        @"class":WKTextFieldItemModel.class,
                        @"placeholder":LLang(@"请输入密码"),
                        @"label": @"密码",
                        @"showBottomLine":@(true),
                        @"password":@(true),
                        @"onChange": ^(NSString *value) {
                            weakSelf.password = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(10.0f),
            @"items":@[
                    @{
                        @"class":WKLabelModel.class,
                        @"text":LLang(@"没有账号？去注册"),
                        @"left": @(20.0f),
                        @"showArrow": @(false),
                        @"textColor": WKApp.shared.config.themeColor,
                        @"onClick":^{
                            WKUsernameRegisterVC *vc = [WKUsernameRegisterVC new];
                            [WKNavigationManager.shared pushViewController:vc animated:YES];
                            
                        },
                    },
            ],
        },
        @{
            @"height":@(80.0f),
            @"items":@[
                    @{
                        @"class":WKButtonItemModel2.class,
                        @"title": LLang(@"登录"),
                        @"width": @(150.0f),
                        @"height":@(40.0f),
                        @"onPressed":^{
//                            WKMnemonicsVC *vc = [WKMnemonicsVC new];
//                            [WKNavigationManager.shared pushViewController:vc animated:YES];
                            [weakSelf onLogin];
                        }
                    },
            ],
        },
        @{
            @"height":@(20.0f),
            @"items":@[
                    @{
                        @"class":WKLabelModel.class,
                        @"text":LLang(@"忘记密码"),
                        @"center": @(true),
                        @"showArrow": @(false),
                        @"textColor": WKApp.shared.config.themeColor,
                        @"onClick":^{
                            WKForgetPasswordVC *vc = [WKForgetPasswordVC new];
                            [WKNavigationManager.shared pushViewController:vc animated:YES];
                            
                        },
                    },
            ],
        },
    ];
}

-(void) onLogin {
    UIView *topView = WKNavigationManager.shared.topViewController.view;
    if(!self.username || self.username.length == 0) {
        [topView showHUDWithHide:LLang(@"账号不能为空！")];
        return;
    }
    if(!self.password || self.password.length == 0) {
        [topView showHUDWithHide:LLang(@"密码不能为空！")];
        return;
    }
    [topView showHUD];
    [WKAPIClient.sharedClient POST:@"user/login" parameters:@{
        @"username":self.username,
        @"password": self.password,
    }].then(^(NSDictionary *result){
        [topView hideHud];
        WKLoginResp *resp = (WKLoginResp*)[WKLoginResp fromMap:result type:ModelMapTypeAPI];
        if([WKWeb3Helper existUserWallet:resp.uid]) {
            [WKUsernameLoginVM handleLoginData:resp isSave:YES];
             WKApp.shared.loginInfo.extra[@"finishedWeb3Veritfy"] = @"1";
            [WKApp.shared.loginInfo save];
            [[WKApp shared] invoke:WKPOINT_LOGIN_SUCCESS param:nil];
        } else {
            [WKUsernameLoginVM handleLoginData:resp isSave:NO];
            WKMnemonicsImportVC *vc = [WKMnemonicsImportVC new];
            [WKNavigationManager.shared pushViewController:vc animated:YES];
        }
    }).catch(^(NSError *error){
        [topView switchHUDError:error.domain];
    });
}


+(void) handleLoginData:(WKLoginResp*)resp isSave:(BOOL)isSave{
    [WKApp shared].loginInfo.token = resp.token;
    if(resp.imToken) {
        [WKApp shared].loginInfo.imToken = resp.imToken;
    }else {
        [WKApp shared].loginInfo.imToken = resp.token;
    }
    [WKApp shared].loginInfo.uid = resp.uid;
    [WKApp shared].loginInfo.extra[@"name"] = resp.name;
    [WKApp shared].loginInfo.extra[@"zone"] = resp.zone;
    [WKApp shared].loginInfo.extra[@"username"] = resp.username?:@"";
    [WKApp shared].loginInfo.extra[@"phone"] = resp.phone;
    [WKApp shared].loginInfo.extra[@"short_no"] = resp.shortNo;
    [WKApp shared].loginInfo.extra[@"short_status"] = resp.shortStatus;
    [WKApp shared].loginInfo.extra[@"sex"] = resp.sex;
    [WKApp shared].loginInfo.extra[@"server_id"] = resp.serverID;
    [WKApp shared].loginInfo.extra[@"chat_pwd"] = resp.chatPwd;
    if(resp.lockScreenPwd && ![resp.lockScreenPwd isEqualToString:@""]) {
        [WKApp shared].loginInfo.extra[@"lock_screen_pwd"] = resp.lockScreenPwd;
        [WKApp shared].loginInfo.extra[@"lock_after_minute"] = resp.lockAfterMinute?:@(0);
    }else {
        [[WKApp shared].loginInfo.extra removeObjectForKey:@"lock_screen_pwd"];
        [[WKApp shared].loginInfo.extra removeObjectForKey:@"lock_after_minute"];
    }
    
    if(resp.rsaPublicKey) {
        [WKApp shared].loginInfo.extra[@"rsa_public_key"] = resp.rsaPublicKey;
    }
    
   
    if(resp.setting) {
        [WKApp shared].loginInfo.extra[@"setting"] = resp.setting;
    }
    if(resp.avatar && ![resp.avatar isEqualToString:@""]) {
        [WKApp shared].loginInfo.extra[@"avatar"] = resp.avatar;
    }else{
        NSString *avatarURL = [WKAvatarUtil getAvatar:resp.uid];
        [WKApp shared].loginInfo.extra[@"avatar"] = avatarURL;
    }
    if(isSave) {
        [[WKApp shared].loginInfo save];
    }
}


@end
